extends Node2D

# 雨天效果属性
@export var duration: float = 15.0  # 雨天持续时间
@export var healing_amount: int = 1  # 每秒治疗量

var time_alive: float = 0.0
var heal_timer: float = 0.0
var player = null

func _ready():
	# 获取玩家引用
	player = get_tree().get_first_node_in_group("player")
	
	# 设置粒子效果
	if has_node("CPUParticles2D"):
		$CPUParticles2D.emitting = true
		$CPUParticles2D.amount = 500
		$CPUParticles2D.lifetime = 1.0
		$CPUParticles2D.emission_shape = CPUParticles2D.EMISSION_SHAPE_RECTANGLE
		$CPUParticles2D.emission_rect_extents = Vector2(get_viewport_rect().size.x, 10)
		$CPUParticles2D.direction = Vector2(0, 1)
		$CPUParticles2D.gravity = Vector2(0, 980)
		$CPUParticles2D.initial_velocity_min = 200
		$CPUParticles2D.initial_velocity_max = 300
		$CPUParticles2D.scale_amount = 1.0
		$CPUParticles2D.color = Color(0.7, 0.8, 1.0, 0.7)
	
	# 播放音效（如果有）
	if has_node("AudioStreamPlayer2D"):
		$AudioStreamPlayer2D.play()

func _process(delta):
	# 更新生命周期
	time_alive += delta
	
	# 治疗计时器
	heal_timer += delta
	if heal_timer >= 1.0:  # 每秒治疗一次
		heal_timer = 0.0
		heal_player()
	
	# 到达最大存活时间后消失
	if time_alive >= duration:
		# 淡出效果
		if has_node("CPUParticles2D"):
			$CPUParticles2D.emitting = false
			await get_tree().create_timer(1.0).timeout  # 等待粒子消失
		queue_free()

func heal_player():
	# 治疗玩家
	if player and is_instance_valid(player):
		if player.has_method("heal"):
			player.heal(healing_amount)
